<template>
  <div>
    <van-nav-bar
      title="登陆"
      left-text="返回"
      left-arrow
      @click-left="goBack"
    />

    <van-form @submit="onSubmit">
      <van-field
        v-model="name"
        name="name"
        label="用户名"
        placeholder="用户名"
        required
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="pwd"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        required
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from "@/apis/login.js";
export default {
  data() {
    return {
      name: "",
      pwd: "",
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    async onSubmit({ name, pwd }) {
      let res = await this.$http.request({
        url: login,
        method: "POST",
        data: {
          name,
          pwd,
        },
      });
      if (res.data.code === 1000) {
        //登陆成功
        this.$toast.success(res.data.msg);
        this.$setStorage("jwtToken", res.data.token);
        this.$router.push("/user");
      }
    },
  },
};
</script>

<style></style>
